<?php

namespace App\Logic;
use App\Logic\JoinLogic;
use Common\Logic\BaseLogic;

class GatheringTourLogic extends BaseLogic
{

    private $mid = 737;
    private $token = '0c0e0ce4b7c3f085eb1d47b307fbad56';
    private $url="http://api.juxiangwan.com/";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取mid.
     *
     * @return void
     */
    public function getMid()
    {
        return $this->mid;
    }

    function get_device_type()
    {
        //全部变成小写字母
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 'other';
        //分别进行判断
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            $type = 'ios';
        } 
  
        if(strpos($agent, 'android'))
        {
            $type = 'android';
        }
        return $type;
    }
    /**
     * 跳转注册聚享玩
     *
     * @return string
     */
    public function getRegisterUrl($userId,$deviceCode){
        
        $from="h5android";
        if($this->get_device_type()=='ios'){
            $from="h5ios";
        }
        $data['resource_id']=$userId;
        return $this->url.'?act=fast_reg&act_type=1&mid='.$this->mid
        .'&resource_id='.$userId
        .'&sign='.$this->getRegisterSign($data)
        .'&device_code='.$deviceCode.'&from='.$from.'&version=2.0';
    }

    /**
     * 接收领奖通知
     *
     * @return string
     */
    public function award($data){
        if($this->checkSign($data)){
            
            $prize_infos=json_decode(htmlspecialchars_decode(urldecode($data['prize_info'])),true);
            if(!empty($prize_infos) && count($prize_infos)>0){
                foreach($prize_infos as $value){
                    //$value数据为：
                    //prize_id	奖励流水标识，唯一
                    //name	广告名称
                    //title	奖励名称
                    //type	任务类别，1试玩，2竞技，3充值，4榜单，7悬赏 8活动
                    //task_prize	领取金额（单位元）
                    //deal_prize	渠道利润（单位元）
                    //task_prize_coin	领取奖励（渠道方货币单位）
                    $value['resource_id']=$data['resource_id'];
                    $joinLogic = new JoinLogic();
                    $result=$joinLogic->joinGatheringTourTask($value);
                    
                    $logFileName = RUNTIME_PATH.'Logs/gt/'.date('Y-m-d').'.log';
                    logs(array(
                        'msg' => '结果处理',
                        'data' => $result
                    ), $logFileName);
                }
            }
            echo "success";//成功统一返回,不然会重发
        }else{
            echo "fail";
        }
    }

    /**
     * 检查签名信息
     *
     * @return boolean
     */
    public function checkSign($data)
    {
		$md5_data=htmlspecialchars_decode(urldecode($data['prize_info'])).$this->mid.$data["time"].$data["resource_id"].$this->token;
		$logFileName = RUNTIME_PATH.'Logs/gt/'.date('Y-m-d').'.log';
                    logs(array(
                        'msg' => '验签',
                        'data' => $md5_data
                    ), $logFileName);
        return $data["sign"]===md5($md5_data);
    }
    /**
     * 获取签名信息
     *
     * @return string
     */
    public function getRegisterSign($data){
        return md5($this->mid.$data['resource_id'].$this->token);
    }
}
